package com.atguigu.tingshu.user.strategy;

import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("1001")
public class AlbumStrategy implements ItemTypeStrategy{
    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;
    @Autowired
    private UserInfoService userInfoService;
    @Override
    public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        LambdaQueryWrapper<UserPaidAlbum> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserPaidAlbum::getAlbumId,userPaidRecordVo.getItemIdList()).eq(UserPaidAlbum::getUserId,userPaidRecordVo.getUserId());
        UserPaidAlbum userPaidAlbum = userPaidAlbumMapper.selectOne(wrapper);
        //如果用户购买了专辑，则直接返回
        if (null != userPaidAlbum){
            return;
        }
        //用户没有购买专辑，则添加记录
        userPaidAlbum = new UserPaidAlbum();
        userPaidAlbum.setOrderNo(userPaidRecordVo.getOrderNo());
        userPaidAlbum.setUserId(userPaidRecordVo.getUserId());
        userPaidAlbum.setAlbumId(userPaidRecordVo.getItemIdList().get(0));
        userPaidAlbumMapper.insert(userPaidAlbum);
    }
}
